package com.henghe.frame.auth.config;

import com.henghe.frame.auth.interceptor.AuthInterceptor;
import com.henghe.frame.auth.constants.AuthConstant;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * Description:
 * Author: LiuWenQing
 * Datetime: 2023/3/17 15:46
 */
@Configuration
@ComponentScan({"com.henghe.frame.auth.service", "com.henghe.frame.auth.manager", "com.henghe.frame.auth.runner", "com.henghe.frame.auth.aop"})
@MapperScan("com.henghe.frame.auth.mapper")
@EnableConfigurationProperties({JWTProperties.class})
public class WebAppConfig implements WebMvcConfigurer {

    @Value("${upload.path}")
    private String uploadPath;

    @Value("${jwt.active}")
    private Boolean active;

    @ConditionalOnProperty({"jwt.active"})
    @Bean
    public AuthInterceptor getAuthInterceptor() {
        return new AuthInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        if(active)
            registry.addInterceptor(getAuthInterceptor()).addPathPatterns("/**");
    }

    @Bean
    public WebMvcConfigurer webMvcConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void addResourceHandlers(ResourceHandlerRegistry registry) {
                registry.addResourceHandler(AuthConstant.RESOURCE_PREFIX + "/**").addResourceLocations("file:" + uploadPath + "/");
                registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
                registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
                //                registry.addResourceHandler("doc.html")
//                        .addResourceLocations("classpath:/META-INF/resources/");
//                registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
            }
        };
    }
}
